package org.pms.dao.impl;

import org.pms.dao.AdministratorsDao;
import org.pms.entity.Administrators;
import org.pms.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @Date 2022-12-12
 * @Author 祝世栩
 *
 * 管理员登录Dao接口的实现
 */
public class AdministratorsDaoImpl implements AdministratorsDao {

    /**
     * 获取连接对象
     */
    Connection connection = null ;
    /**
     * 使用连接对象，获取语句对象(PreparedStatement)，并预编译SQL语句
     */
    PreparedStatement ps = null ;
    /**
     * 使用连接对象，获取语句对象(PreparedStatement)，并预编译SQL语句
     */
    ResultSet rs = null ;
    /**
     * 创建实体对象
     */
    Administrators administrators = null ;
    /**
     * 定义操作数据库的SQL语句
     */
    String sql = null ;

    /**
     * 关闭对象
     */
    public void close(){
        DbUtil.close(connection,ps,rs);
    };

    @Override
    public Administrators AdministratorsSelectNumberPwd(String Number, String Pwd) {
         sql = "select Administrators_Number,Administrators_Pwd from Administrators where " +
                "Administrators_Number = ? and Administrators_Pwd = ?";

        try {
            connection = DbUtil.getConnection();
            ps = connection.prepareStatement(sql);
            // 设置数据
            ps.setString(1,Number);
            ps.setString(2,Pwd);

            // 执行SQL语句，并接收返回结果
            rs = ps.executeQuery();
            if (rs.next()){
                // 读取数据
                String AdministratorsNumber = rs.getString(1);
                String AdministratorsPwd = rs.getString(2);
                administrators = new Administrators(AdministratorsNumber,AdministratorsPwd);

            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            new AdministratorsDaoImpl().close();
        }
        // 返回对象
        return administrators;
    }

    @Override
    public List<Administrators> AdministratorsSelectAll() {
        List<Administrators> list = new ArrayList<>();
        sql = "select Administrators_Number,Administrators_Pwd from Administrators";
        try {
            connection = DbUtil.getConnection();
            ps = connection.prepareStatement(sql);
            rs = ps.executeQuery();
            // 对结果进行处理
            while (rs.next()){
                // 读取数据
                String Administrators_Number = rs.getString(1);
                String Administrators_Pwd = rs.getString(2);
                administrators = new Administrators();
                administrators.setAdministratorsNumber(Administrators_Number);
                administrators.setAdministratorsPwd(Administrators_Pwd);
                // 把实体对象添加到List集合中
                list.add(administrators);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            new AdministratorsDaoImpl().close();
        }
        return list;
    }

    @Override
    public int AdministratorsInsert(Administrators administrators) {
        int r = 0;
        sql = "insert into Administrators(Administrators_Number,Administrators_Pwd) values(?,?)";
        try {
            connection = DbUtil.getConnection();
            ps = connection.prepareStatement(sql);
            // 设置数据
            ps.setString(1,administrators.getAdministratorsNumber());
            ps.setString(2,administrators.getAdministratorsPwd());
            // 执行SQL语句，并接收返回结果
            r = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            new AdministratorsDaoImpl().close();
        }
        return r;
    }

    @Override
    public int AdministratorsUpdate(Administrators administrators) {
        int r = 0;
        sql = "update Administrators set Administrators_Pwd = ? where Administrators_Number = ? ";
        try {
            connection = DbUtil.getConnection();
            ps = connection.prepareStatement(sql);
            // 设置数据
            ps.setString(1,administrators.getAdministratorsPwd()) ;
            ps.setString(2,administrators.getAdministratorsNumber()) ;
            // 执行SQL语句，并接收返回结果
            r = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            new AdministratorsDaoImpl().close();
        }
        // 返回受影响行数
        return r;
    }


}
